package org.fuys.coder.domain.auth.service.vertify.permission.repo.impl;

import org.fuys.coder.infrastructure.model.dto.UserPermissionDTO;
import org.fuys.coder.common.constants.RedisConstants;
import org.fuys.coder.infrastructure.util.RedisUtil;
import org.fuys.coder.domain.auth.repository.IPermissionRepository;
import org.fuys.coder.domain.auth.service.holder.PermissionHolder;
import org.fuys.coder.domain.auth.service.vertify.permission.repo.IPermissionRepoServiceAggregation;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.auth.service.vertify.permission.repo.impl
 * @className: PermissionRepoServiceAggregationImpl
 * @author: WangJing
 * @description: 实现类
 * @date: 2024/7/11 11:04
 * @version: 1.0
 */
@Service
public class PermissionRepoServiceAggregationImpl implements IPermissionRepoServiceAggregation {

    @Resource
    private IPermissionRepository permissionRepository;


    @Override
    public List<UserPermissionDTO> getAllRoleDetail() {
        return permissionRepository.getAllRoleDetail();
    }

    @Override
    public boolean checkAuth(Integer userId) {
        try {
            //查看Java的一级缓存中是否存在
            if(!PermissionHolder.hasUser(userId)){
                //不存在则查找redis中数据
                final Object o = permissionRepository.getUserPermission(userId);
                return !ObjectUtils.isEmpty(o);
            }
        } catch (Exception e) {
            //出现任何异常 按照认证失败处理
            e.printStackTrace();
            return false;
        }
        //如果一级缓存查到 返回
        return true;
    }

}
